import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Map;

import Utils.FileUtil;
import Utils.ListUtil;
import Utils.POSTool;
import Utils.RETool;
import Utils.SortUtils;

public class Habit {
	// 抽取习惯
	public static ArrayList<String> extractHabit(String string) {
		ArrayList<String> result = new ArrayList<String>();
		String str = "";
		String path = "c:/weibo/wordlist/habits.txt";
		String  content = FileUtil.readFile(path, "utf-8");
		for (String habit : content.split("\n"))
			str += habit + "|";
		str = str.substring(0, str.length() - 1);

		//多少种抽取规则
		ArrayList<String> expressions = new ArrayList<String>();
		expressions.add("(" + str + ")");

		System.out.println("(" + str + ")*");

		for (int i = 0; i < expressions.size(); i++) {
			//在某种规则下的抽取结果
			ArrayList<String> tmp = RETool.extractRange(string, expressions.get(i));
			result = ListUtil.merge(result, tmp);
		}

		return result;
	}

	// 从文件中学习出新的persons
	public static void findNewPersons(String fileName) {
		Hashtable<String, Integer> word_count = new Hashtable<String, Integer>();
		String content = FileUtil.readFile(fileName, "utf-8");
		String pos = POSTool.POSSentence(content);

		String lines[] = pos.split("\n");
		for (String line : lines) {
			String items[] = line.split(" ");
			for (int i = 0; i < items.length - 1; i++) {
				// 方向指示词
				boolean isPrep = false;
				String[] preps = { "在/p", "站在/v", "躺在/v", "坐在/v", "从/p", "到/p", "回到/v", "返回/v", "去/p", "来/p" };
				for (String prep : preps)
					isPrep = isPrep || items[i].equals(prep);

				boolean isPerson = false;
				String[] persons = { "/ns", "/n", "/r" };
				for (String person : persons)
					isPerson = isPerson || items[i + 1].indexOf(person) > 0;

				if (isPrep && isPerson) {
					String word = items[i + 1];
					if (word_count.containsKey(word))
						word_count.put(word, word_count.get(word) + 1);
					else
						word_count.put(word, 1);
				}
			}
		}
		Map.Entry[] maps = SortUtils.sortByKey(word_count, -1);
		for (int i = 0; i < maps.length; i++)
			System.out.println(maps[i].getKey() + ":" + maps[i].getValue());
	}

	public static void main(String args[]) {
		// 扩充location词典
		// String fileName = "c:/weibo/test/extracts.txt";
		// findNewLocations(fileName);

		String fileName = "c:/weibo/test/extracts.txt";
		String content = FileUtil.readFile(fileName, "utf-8");
		ArrayList<String> habits = extractHabit(content);
		System.out.println(habits);
	}
}
